home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / et / et3_0-a1.lha / et3 / src / System.C < prev    next >
C/C++ Source or Header  |  1992-08-07  |  6KB  |  375 lines

  1. #ifdef __GNUG__
  2. #pragma implementation
  3. #endif
  4.  
  5. #include "System.h"
  6.  
  7. #include "Class.h"
  8. #include "CLib.h"
  9. #include "String.h"
  10. #include "Error.h"
  11. #include "Directory.h"  
  12. #include "OrdColl.h"
  13. #include "Env.h"
  14. #include "Point.h"
  15.  
  16. char    *gEtDir;
  17. System  *gSystem;
  18. char    *gProgname= "*** progname not set ***";
  19. bool     gInMain;
  20. int      gDebug;
  21. bool     gQuitApp;
  22.  
  23. static bool cleanuplock;
  24.  
  25. void ETInit(char **argv)
  26. {
  27.     char *a0;
  28.     
  29.     if (gSystem)
  30.     return;
  31.     
  32.     if (argv && argv[0])
  33.     gProgname= argv[0];
  34.     
  35.     gInMain= TRUE;
  36.     InitSystem();
  37.  
  38.     gEtDir= CLib::Getenv("ET_DIR");
  39.     if (gEtDir == 0)
  40.     gEtDir= "/local/et";
  41.  
  42.     for (int i= 0; a0= argv[i]; i++) {
  43.     if (a0[0] == '-' && a0[1] == 'E' && a0[2]) {
  44.         a0= &a0[2];
  45.         if (strcmp(a0, "d") == 0)
  46.         a0= "Debug";
  47.         Env::SetValue(a0, eEnvCommand);
  48.         gDebug= Env::GetValue("System.Debug", 0);
  49.     }
  50.     }
  51.     
  52.     gDebug= Env::GetValue("System.Debug", 0);
  53.  
  54.     if (Env::GetValue("System.MemStat", 0))
  55.     Storage::EnableStatistics();
  56.     int msize= Env::GetValue("System.MemStat.size", -1);
  57.     int mcnt= Env::GetValue("System.MemStat.cnt", -1);
  58.     if (msize != -1 || mcnt != -1)
  59.     Storage::EnableStatistics(msize, mcnt);
  60. }
  61.  
  62. void ETCleanup()
  63. {
  64.     SafeDelete(gSystem);
  65. }
  66.  
  67. //---- SysEvtHandler -----------------------------------------------------------
  68.  
  69. NewAbstractMetaImpl(SysEvtHandler, Object, (T(resourceId), TP(owner)));
  70.  
  71. SysEvtHandler::SysEvtHandler(int id)
  72. {
  73.     owner= 0;
  74.     resourceId= id;
  75. }
  76.  
  77. SysEvtHandler::~SysEvtHandler()
  78. {
  79.     if (!cleanuplock && owner) {
  80.     owner->ForEach(SeqCollection,RemovePtr)(this);
  81.     }
  82.     SafeDelete(owner);
  83. }
  84.  
  85. void SysEvtHandler::Notify(SysEventCodes, int)
  86. {
  87. }
  88.  
  89. bool SysEvtHandler::HasInterest(SysEventCodes code)
  90. {
  91.     return owner != 0;
  92. }
  93.  
  94. void SysEvtHandler::AddOwner(SeqCollection *c)
  95. {
  96.     if (owner == 0)
  97.     owner= new OrdCollection;
  98.     if (owner->FindPtr(c) == 0)
  99.     owner->Add(c);
  100. }
  101.  
  102. bool SysEvtHandler::IsEqual(Object *op)
  103. {
  104.     return resourceId == ((SysEvtHandler*)op)->resourceId;
  105. }
  106.  
  107. void SysEvtHandler::Remove()
  108. {
  109.     if (owner) {
  110.     owner->ForEach(SeqCollection,RemovePtr)(this);
  111.     SafeDelete(owner);
  112.     }
  113.     gSystem->AddCleanupObject(this);
  114. }
  115.  
  116. int SysEvtHandler::Error(int code)
  117. {
  118.     return 0;
  119. }
  120.  
  121. //---- System ------------------------------------------------------------------
  122.  
  123. NewMetaImpl(System, Object, (TS(errorstr), TS(osid), TB(done), TP(fileInputHandler),
  124.     TP(fileOutputHandler), TP(zombieHandler), TP(signalHandler),
  125.     TP(asyncSignalHandler), TP(timeoutHandler), TP(cleanupList),
  126.     TS(gEtDir), TS(gProgname), TB(gInMain), TB(gDebug), TB(gQuitApp),
  127.     TSP(CLib::Environ)));
  128.  
  129. System::System(char *name)
  130. {
  131.     osid= name;
  132. }
  133.  
  134. bool System::Init()
  135. {
  136.     fileInputHandler    = new OrdCollection;
  137.     fileOutputHandler   = new OrdCollection;
  138.     zombieHandler       = new OrdCollection;
  139.     signalHandler       = new OrdCollection;
  140.     asyncSignalHandler  = new OrdCollection;
  141.     timeoutHandler      = new OrdCollection;
  142.     workHandler         = new OrdCollection;
  143.     cleanupList= 0;
  144.  
  145.     return FALSE;
  146. }
  147.  
  148. static void remove(SeqCollection *&oc)
  149. {
  150.     if (oc) {
  151.     Iter next(oc);
  152.     register SysEvtHandler *seh;
  153.     while (seh= (SysEvtHandler*) next())
  154.         gSystem->AddCleanupObject(seh);
  155.     SafeDelete(oc);
  156.     }
  157. }
  158.  
  159. System::~System()
  160. {
  161.     remove(fileInputHandler);
  162.     remove(fileOutputHandler);
  163.     remove(zombieHandler);
  164.     remove(signalHandler);
  165.     remove(asyncSignalHandler);
  166.     remove(timeoutHandler);
  167.     remove(workHandler);
  168.  
  169.     if (gSystem == this)
  170.     gSystem= 0;
  171.     
  172.     /*
  173.     Remove();
  174.     */
  175.     if (cleanupList) {
  176.     cleanuplock= TRUE;
  177.     cleanupList->FreeAll();
  178.     cleanuplock= FALSE;
  179.     SafeDelete(cleanupList);
  180.     }
  181. }
  182.  
  183. void System::Control()
  184. {
  185.     done= FALSE;
  186.     while (! done) {
  187.     InnerLoop(100);
  188.     if (cleanupList)
  189.         Remove();
  190.     }
  191. }
  192.  
  193. void System::ExitControl()
  194. {
  195.     gQuitApp= done= TRUE;
  196. }
  197.  
  198. void System::InnerLoop(int)
  199. {
  200.     AbstractMethod("InnerLoop");
  201. }
  202.  
  203. Directory *System::MakeDirectory(char *name)
  204. {
  205.     return new Directory(name);
  206. }
  207.  
  208. int System::GetPathInfo(char*, int*, long*, int*, long*)
  209. {   
  210.     AbstractMethod("GetPathInfo");
  211.     return -1;
  212. }
  213.  
  214. bool System::AccessPathName(char*, int)
  215. {
  216.     return FALSE;
  217. }
  218.  
  219. bool System::ExpandPathName(char*, int)
  220. {
  221.     return FALSE;
  222. }
  223.     
  224. bool System::ChangeDirectory(char *)
  225. {
  226.     return FALSE;
  227. }
  228.     
  229. char *System::WorkingDirectory()
  230. {
  231.     return 0;
  232. }
  233.  
  234. char *System::HomeDirectory()
  235. {
  236.     return 0;
  237. }
  238.  
  239. void System::Rename(char *from, char *to)
  240. {
  241.     CLib::Unlink(to);
  242.     if (CLib::Link(from, to) != -1) 
  243.     CLib::Unlink(from);
  244. }
  245.  
  246. void System::Wait(unsigned int)
  247. {
  248. }
  249.  
  250. int System::GetPid()
  251. {
  252.     return -1;
  253. }
  254.  
  255. bool System::CanRead(int, int)
  256. {
  257.     return FALSE;
  258. }
  259.  
  260. bool System::CanWrite(int, int)
  261. {
  262.     return FALSE;
  263. }
  264.  
  265. void System::Remove()
  266. {
  267.     while (cleanupList != 0) {
  268.     SeqCollection *tmp= cleanupList;
  269.     cleanupList= 0;
  270.     tmp->FreeAll();
  271.     delete tmp;
  272.     }
  273. }
  274.  
  275. void System::AddHandler(SeqCollection *s, SysEvtHandler *h)
  276. {
  277.     s->Add(h);
  278.     h->AddOwner(s);
  279. }
  280.  
  281. SysEvtHandler *System::RemoveHandler(SeqCollection *s, SysEvtHandler *h)
  282. {
  283.     return (SysEvtHandler*) s->RemovePtr(h);
  284. }
  285.  
  286. void System::AddCleanupObject(Object *op)
  287. {
  288.     if (cleanupList == 0)
  289.     cleanupList= new OrdCollection;
  290.     if (cleanupList->FindPtr(op) == 0)
  291.     cleanupList->Add(op);
  292. }
  293.  
  294. void System::Abort(int)
  295. {
  296.     CLib::Abort();
  297. }
  298.  
  299. void System::Exit(int code, bool mode)
  300. {    
  301.     if (mode)
  302.     CLib::Exit(code);
  303.     CLib::FastExit(code);
  304. }
  305.  
  306. PttyConnection* System::MakePttyConnection(char *, char **)
  307. {
  308.     return 0;
  309. }
  310.  
  311. //---- environment manipulation ------------------------------------------------
  312.  
  313. char *System::Getenv(char*)
  314. {
  315.     AbstractMethod("Getenv");
  316.     return 0;
  317. }
  318.  
  319. void System::Setenv(char*, char*)
  320. {
  321.     AbstractMethod("Setenv");
  322. }
  323.  
  324. void System::Unsetenv(char *name)
  325. {
  326.     Setenv(name, 0);
  327. }
  328.  
  329. Object *System::Load(char*, char*)
  330. {
  331.     AbstractMethod("Load");
  332.     return 0;
  333. }
  334.  
  335. char *System::GetError()
  336. {
  337.     return form("errno: %d", CLib::ErrNo);
  338. }
  339.  
  340. char *System::DirName(char *pathname) 
  341. {
  342.     if (strchr(pathname, '/')) {
  343.     static char buf[1000];
  344.     strcpy(buf, pathname);
  345.     char *r= strrchr(buf, '/');
  346.     if (r != buf)
  347.         *r= '\0';
  348.     return buf;
  349.     }
  350.     return WorkingDirectory();
  351. }
  352.  
  353. //---- RPC ---------------------------------------------------------------------
  354.  
  355. int System::OpenConnection(char*, char*)
  356. {
  357.     return -1;
  358. }
  359.  
  360. int System::AnnounceTcpService(char*)
  361. {
  362.     return -1;
  363. }
  364.  
  365. int System::AnnounceUnixService(char*)
  366. {
  367.     return -1;
  368. }
  369.  
  370. int System::AcceptConnection(int)
  371. {
  372.     return -1;
  373. }
  374.  
  375.